package com.mworld.galaxy.bull.entity.product;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "t_link")
@AssociationOverrides({
    @AssociationOverride(name = "pk.category", joinColumns = @JoinColumn(name = "c_category_id")),
    @AssociationOverride(name = "pk.product", joinColumns = @JoinColumn(name = "c_product_id"))
})
public class Link implements Serializable {
    private static final long serialVersionUID = 1837214990350930819L;

    private LinkId pk = new LinkId();
    private Date createDate;

    public Link() {
    }

    @EmbeddedId
    public LinkId getPk() {
        return pk;
    }

    public void setPk(LinkId pk) {
        this.pk = pk;
    }

    @Transient
    public Category getCategory() {
        return pk.getCategory();
    }

    public void setCategory(Category category) {
        pk.setCategory(category);
    }

    @Transient
    public Product getProduct() {
        return pk.getProduct();
    }

    public void setProduct(Product product) {
        pk.setProduct(product);
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "c_create_date")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Override
    @Transient
    public int hashCode() {
        return pk == null ? 0 : pk.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Link)) {
            return false;
        }
        Link link = (Link) obj;
        if (pk == null || link.getPk() == null) {
            return pk == link.getPk();
        }
        return pk.equals(link.getPk());
    }
}
